package com.zeny.mycrm.controller;

import com.zeny.mycrm.model.CourseOrder;
import com.zeny.mycrm.model.PageResult;
import com.zeny.mycrm.service.CourseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName CourseOrderController
 * @Description 课程订单
 * @Author zeny
 */
@Controller
@RequestMapping("/courseorder")
public class CourseOrderController {

    @Autowired
    private CourseOrderService courseOrderService;

    @GetMapping("/list")
    public String list() {
        return "courseorder/order";
    }

    @GetMapping("/add")
    public String add() {
        return "courseorder/add";
    }

    @GetMapping("/detail")
    public String detail(String order_id, Model model) {
        CourseOrder courseOrder = courseOrderService.findByOrderId(order_id);
        model.addAttribute("courseOrder", courseOrder);
        return "courseorder/detail";
    }


    @GetMapping("/edit")
    public String edit(String order_id, Model model) {
        CourseOrder courseOrder = courseOrderService.findByOrderId(order_id);
        model.addAttribute("courseOrder", courseOrder);
        return "courseorder/edit";
    }

    @ResponseBody
    @PostMapping("/update")
    public PageResult update(CourseOrder courseOrder) {
        if (!isVlaid(courseOrder) || StringUtils.isEmpty(courseOrder.getOrder_id())) {
            return new PageResult().code(2);
        }
        courseOrderService.update(courseOrder);
        return new PageResult().code(1);
    }

    @ResponseBody
    @PostMapping("/del")
    public PageResult del(String order_id) {
        courseOrderService.delByOrderId(order_id);
        return new PageResult().code(1);
    }

    @ResponseBody
    @GetMapping("/listJson")
    public PageResult listJson(CourseOrder courseOrder,
            @RequestParam(defaultValue = "1", required = true) Integer page,
            @RequestParam(defaultValue = "10", required = true) Integer limit) {

        return courseOrderService.findListByMap(courseOrder, page, limit).msg("").code(0);
    }

    @ResponseBody
    @PostMapping("/save")
    public PageResult save(CourseOrder courseOrder) {
        if (!isVlaid(courseOrder)) {
            return new PageResult().code(2);
        }
        courseOrderService.save(courseOrder);
        return new PageResult().code(1);
    }

    private boolean isVlaid(CourseOrder courseOrder) {
        if (courseOrder == null
                || StringUtils.isEmpty(courseOrder.getCourse_name())
                || StringUtils.isEmpty(courseOrder.getCourse_price())
                || StringUtils.isEmpty(courseOrder.getName())
                || StringUtils.isEmpty(courseOrder.getOrder_date())
                || StringUtils.isEmpty(courseOrder.getQq_no())
                || StringUtils.isEmpty(courseOrder.getTel())
                || StringUtils.isEmpty(courseOrder.getWechat_no())
                || StringUtils.isEmpty(courseOrder.getWechat_mark())) {
            return false;
        }
        return true;
    }



}
